/**
 * @author Administrator
 */

window.onload = function (){
	
	
	// 全局game对象, 也即程序层, 保存canvas  Physics SceneManager以及Logic对象, 完成资源的加载工作.
	// 同时使用状态机实现程序状态控制:
	// 使用单例的形式构建
	var DangoJump =  {
		mCanvas : undefined,
		mContext : undefined,
		mLogic : undefined,
		mPhysics : undefined,
		mSceneManager : undefined,
		mGUI : undefined,
		mTime : undefined,
		mTimer : undefined,
		// 状态机指示变量
		mStart	: false,
		//  如果资源(图片)全部加载完成, 则这个值就为真
		mInited : undefined,
		//  帧速.
		mFrameSpeed : 100/6,
		//  这个函数用来给资源的onload回调
		Loading : function() {
			DangoJump.mTime += DangoJump.mFrameSpeed;
			
			
			// 确认所有程序运行的必须元素如Logic physics之类的类已经载入完成
			// 可以在这里做载入画面~~
			// 比如:::
			DangoJump.mContext.save(); //把当前的绘图状态保存起来(如旋转角度的初始位置, 填充颜色, 坐标原点等)  
			DangoJump.mContext.clearRect(0,0,DangoJump.mCanvas.width, DangoJump.mCanvas.height);//擦除画布  
			DangoJump.mContext.translate(DangoJump.mCanvas.width/2, DangoJump.mCanvas.height/2);//把坐标原点移动到画布中央  
			DangoJump.mContext.fillStyle = 'transparent';  
			DangoJump.mContext.fillStyle = 'black';
			
			var base = DangoJump.mTime / DangoJump.mFrameSpeed ;
			base %= 12;
			var angle = Math.PI / 6;  
			var beginAngle = angle * base ;  
			for(var i = 1; i <= 12; i ++){  
           		DangoJump.mContext.beginPath();//开始一个路径  
				if(i === 1){  
					DangoJump.mContext.rotate(beginAngle);  
				}else{  
					DangoJump.mContext.rotate(angle);//每次调用rotate之后, 其旋转角度并不会还原, 而是接着上一次的位置  
				}  
				
				DangoJump.mContext.arc(0, -48, i * 0.8 + 1, 0, 2 * Math.PI, true);//绘制一个圆点  
				DangoJump.mContext.closePath();//闭合路径  
				//如果不是用beginPath和closePath, 在调用fill方法时, 会导致画布上的所有圆都重叠在一起了  
				DangoJump.mContext.fill();//填充(使用上面最后定义的fillStyle)  
			}
			DangoJump.mContext.restore();;
			
			
			// 最后如果全部完成
			if(  DangoJump.mLogic.ready() 
				&& DangoJump.mPhysics.ready()
				&& DangoJump.mSceneManager.ready()
				&& DangoJump.mGUI.ready()
				 )
			{
					DangoJump.mInited = true;
					window.clearInterval(DangoJump.mTimer);
					DangoJump.mTimer = window.setInterval( DangoJump.Run, DangoJump.mFrameSpeed);
			}

		},

		Run : function() {

					 DangoJump.mTime += DangoJump.mFrameSpeed;
					 DangoJump.mLogic.update(DangoJump.mFrameSpeed);
			// 		 /更新画面
					 DangoJump.mSceneManager.draw();
					 DangoJump.mGUI.draw();

		},
		Init : function(){
			//var _this = this;
			DangoJump.mCanvas = document.getElementById("myCanvas");
			DangoJump.mContext = DangoJump.mCanvas.getContext("2d");
			
			DangoJump.mTime = 0;
			
			DangoJump.mSceneManager = new SceneManager(DangoJump.mCanvas.width, DangoJump.mCanvas.height, this.mCanvas);
			
			DangoJump.mPhysics = new Physics();
			
			DangoJump.mGUI = new GUI(DangoJump.mCanvas,DangoJump.mCanvas);
			
			DangoJump.mLogic = new Logic(DangoJump.mSceneManager,DangoJump.mPhysics,DangoJump.mGUI);
			

			
			DangoJump.mTimer = window.setInterval(DangoJump.Loading, DangoJump.mFrameSpeed );
		}
		
	}
	DangoJump.Init();
}